package com.yangcangclub.commons.utils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/5/5.
 */
public class ListUtil {
    
    
    public static List removeNull(List list){
        if(list==null||list.size()==0){return Lists.newArrayList();}
        List ll=Lists.newArrayList();
        for(Object o:list){
            if(o==null){
                continue;
            }
            ll.add(o);
        }
        return ll;
    }



    /**
     * list转map
     * @param list
     * @param key 指定的key
     * @return
     */
    public static Map<Integer,Map<String ,Object>> listToMapByKey(List<Map<String,Object>> list,String key){
        if(list==null||list.size()==0){return Maps.newHashMap();}

        Map<Integer,Map<String ,Object>> map=Maps.newHashMap();
        for(Map m:list){
            map.put((Integer)m.get(key),m);
        }
        return map;

    }

    /**
     * 从列表中获取指定key 的Ids列表集合,Map里必须有指定key
     * @param list
     * @return
     */
    public static  List<Integer> getIdsFromListByKey(List<Map<String,Object>> list,String key){
        List<Integer> ll=Lists.newArrayList();
        if(list==null){return ll;}
        for(Map a:list){
            ll.add((Integer)a.get(key));
        }
        return ll;
    }


    
    
}
